package com.infore.dataStatistics.main.utils;

import cn.hutool.extra.spring.SpringUtil;
import com.infore.common.enums.DatabaseDriverClass;
import com.infore.common.model.entity.TBaseDatasource;
import com.infore.dataStatistics.main.dto.DataSourceDto;
import com.infore.dataStatistics.main.dto.StatTask;
import com.infore.dataStatistics.main.service.TBaseDatasourceService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * 数据源处理工具类
 */
@Slf4j
public class DataSourceUtils  {

    private JdbcUtils sourceJdbc;

    private JdbcUtils targetJdbc;

    private TBaseDatasourceService tBaseDatasourceService = SpringUtil.getBean(TBaseDatasourceService.class);

    public List<Map<String, Object>> getDataList(StatTask task){

        DataSourceDto sourceDto = task.getSourceDto();

        if (sourceDto == null){
            log.error("目标数据源为空:" + task.toString());
            return null;
        }

        TBaseDatasource sourceData = tBaseDatasourceService.selectById(sourceDto.getDataSourceId());
        DatabaseDriverClass databaseDriverClass = null;
        if( sourceData.getDatabaseType().toUpperCase().equals(DatabaseDriverClass.MYSQL.getName())){
            databaseDriverClass = DatabaseDriverClass.MYSQL;
        }else if(sourceData.getDatabaseType().toUpperCase().equals(DatabaseDriverClass.MYSQL5.getName())){
            databaseDriverClass = DatabaseDriverClass.MYSQL5;
        }else if(sourceData.getDatabaseType().toUpperCase().equals(DatabaseDriverClass.ORACLE.getName())){
            databaseDriverClass = DatabaseDriverClass.ORACLE;
        }
        sourceJdbc = new JdbcUtils(databaseDriverClass.getDriverClass(), sourceData.getJdbcUrl(),
                sourceData.getJdbcUsername(), sourceData.getJdbcPassword());
        String sql = "SELECT * FROM " + sourceDto.getTableName() + " WHERE stat_flag = 0";

        return sourceJdbc.executeQuery(sql);
    }


    public int updateFlag(List<Map<String, Object>> dataList, StatTask task) {
        return 0;
    }

    public int saveDataList(List<Map<String, Object>> dataList, StatTask task) {
        return 0;
    }

}
